﻿@using Masa.Blazor

<MForm Model="@Location">
    <MRow>
        <MCol Cols="12" Sm="6">
            <MTextField @bind-Value="Location.Path"
            Label="路径"
            Rules="@PathRules"
            Outlined />
        </MCol>

        <MCol Cols="12" Sm="6">
            <MSelect @bind-Value="LocationType"
            ItemText="item => item"
            ItemValue="item => item"
            Label="位置类型"
            Items="@(new List<string> { "代理", "根目录", "别名" })"
            Outlined>
            </MSelect>
        </MCol>
    </MRow>

    <!-- 根据选择的类型显示不同配置 -->
    @if (LocationType == "代理")
    {
        <MRow>
            <MCol Cols="12">
                <MTextField @bind-Value="Location.ProxyPass"
                Label="代理目标"
                Rules="@ProxyPassRules"
                Outlined />
            </MCol>
        </MRow>
    }
    else if (LocationType == "根目录")
    {
        <MRow>
            <MCol Cols="12">
                <MTextField @bind-Value="Location.Root"
                Label="根目录路径"
                Outlined />
            </MCol>
        </MRow>
    }
    else if (LocationType == "别名")
    {
        <MRow>
            <MCol Cols="12">
                <MTextField @bind-Value="Location.Alias"
                Label="别名路径"
                Outlined />
            </MCol>
        </MRow>
    }

    <MRow>
        <MCol Cols="12">
            <MCheckbox @bind-Value="Location.IsWebSocket"
            Label="启用 WebSocket 支持"
            Class="mt-3" />
        </MCol>
    </MRow>



    <MRow>
        <MCol Cols="12">
            <span class="text-subtitle-2">额外指令</span>
            @if (Location.AdditionalDirectives != null)
            {
                @for (int i = 0; i < Location.AdditionalDirectives.Count; i++)
                {
                    var dir = Location.AdditionalDirectives[i];
                    <MRow Class="d-flex align-center mt-2">
                        <MCol Cols="11">
                            <MTextField @bind-Value="dir"
                                        Outlined
                                        Class="flex-grow-1" />
                        </MCol>
                        <MCol Cols="1">
                            <MIconButton Icon="mdi-delete"
                                         Color="error"
                                         OnClick="() => RemoveLocationDirective(Location.AdditionalDirectives[i])" />
                        </MCol>
                    </MRow>
                }
            }
            <MButton Outlined
                     Color="primary"
                     Class="mt-2"
                     OnClick="AddLocationDirective"
                     PrependIcon="mdi-plus">
                添加指令
            </MButton>
        </MCol>
    </MRow>

    <MRow>
        <MCol Cols="12" Class="mt-4">
            <MButton Text
                     Color="error"
                     Class="float-right"
                     OnClick="OnRemove"
                     PrependIcon="mdi-delete">
                删除此路径配置
            </MButton>
        </MCol>
    </MRow>
</MForm>

@code {
    [Parameter]
    public NginxServerConfigLocation Location { get; set; }

    [Parameter]
    public EventCallback OnRemove { get; set; }

    private string LocationType
    {
        get
        {
            if (!string.IsNullOrEmpty(Location.ProxyPass)) return "代理";
            if (!string.IsNullOrEmpty(Location.Root)) return "根目录";
            if (!string.IsNullOrEmpty(Location.Alias)) return "别名";
            return "代理";
        }
        set
        {
            // 切换类型时清空其他配置
            if (value == "代理")
            {
                Location.Root = null;
                Location.Alias = null;
            }
            else if (value == "根目录")
            {
                Location.ProxyPass = null;
                Location.Alias = null;
            }
            else if (value == "别名")
            {
                Location.ProxyPass = null;
                Location.Root = null;
            }
        }
    }

    // 验证规则
    private List<Func<string, StringBoolean>> PathRules => new()
    {
        v => !string.IsNullOrWhiteSpace(v) ? null : "路径不能为空"
    };

    private List<Func<string, StringBoolean>> ProxyPassRules => new()
    {
        v => !string.IsNullOrWhiteSpace(v) ? null : "代理目标不能为空"
    };

    private void AddLocationDirective()
    {
        Location.AdditionalDirectives.Add("");
    }

    private void RemoveLocationDirective(string directive)
    {
        Location.AdditionalDirectives.Remove(directive);
    }
}